/**
 * @FileName: FindPasswordActivity.java
 * @Package:com.bdtop.dbl.ui.activities.acccount
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-15
 * @version V1.0
 */

package com.bdtop.dbl.ui.activities.acccount;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.bdtop.dbl.BaseActivity;
import com.bdtop.dbl.R;
import com.bdtop.dbl.server.ServerApiFactory;
import com.bdtop.dbl.server.response.BaseResponse;
import com.bdtop.dbl.thirdparty.asynchttp.RequestCallback;
import com.bdtop.dbl.ui.view.TitleBar;
import com.bdtop.dbl.utils.Constants;
import com.bdtop.dbl.utils.NetworkUtils;
import com.bdtop.dbl.utils.Preferences;

/**
 * @ClassName: FindPasswordActivity
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-15
 */
public class FindPwdActivity extends BaseActivity implements OnClickListener {

    private TitleBar mTitleBar;
    private Button btnConfirm;
    private EditText editCode;
    private EditText password, rePassword;
    private final static String mobile_num = "mobile";
    private Button mReVerifyBtn;
    private ButtonTimer mButtonTimer;

    public static final void launch(Context context, String mobile) {
        Intent intent = new Intent(context, FindPwdActivity.class);
        intent.putExtra(mobile_num, mobile);
        ((Activity) context).startActivityForResult(intent, 0);
    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_reset_password);
        initViews();
        mButtonTimer = new ButtonTimer(60 * 1000, 1000);
        mReVerifyBtn.setEnabled(false);
        if (mButtonTimer != null) {
            mButtonTimer.start();
        }
        mReVerifyBtn.setOnClickListener(this);
    }

    private void initViews() {
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.setTitle(R.string.find_pwd);
        mTitleBar.setBelongActivity(this);
        
        mReVerifyBtn = (Button) findViewById(R.id.re_verify_btn);

        password = (EditText) findViewById(R.id.edit_password);
        rePassword = (EditText) findViewById(R.id.edit_re_password);

        btnConfirm = (Button) findViewById(R.id.btn_confirm);
        btnConfirm.setOnClickListener(this);

        editCode = (EditText) findViewById(R.id.code);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.re_verify_btn:
                mReVerifyBtn.setEnabled(false);
                if (mButtonTimer == null) {
                    mButtonTimer = new ButtonTimer(60 * 1000, 1000);
                }
                String phoneNumber = getIntent().getStringExtra(mobile_num);
                showWaitingDialog(R.string.waiting_get_code, false, true);
                ServerApiFactory.getApiImpl().getAuthCode(
                        phoneNumber, new RequestCallback() {

                            @Override
                            public void onSuccess(String rawData, Object response) {
                                BaseResponse baseResponse = (BaseResponse) response;
                                if (baseResponse.getStatus()
                                        .equals(Constants.REQUEST_SUCCESS)) {
                                    mButtonTimer.start();
                                } else {
                                    showToast(getString(R.string.get_code_error));
                                }
                                dismissDialog();
                            }

                            @Override
                            public void onError(int statusCode, Throwable e, String rawData) {
                                if (NetworkUtils.isNetConnect(getApplicationContext())) {
                                    showToast(R.string.oper_error);
                                } else {
                                    showToast(R.string.net_error);
                                }
                                dismissDialog();
                            }
                        });
                break;
            case R.id.btn_confirm:
                if (check()) {
                    final String name = getIntent().getStringExtra(mobile_num);
                    final String pwd = password.getText().toString().trim();
                    String code = editCode.getText().toString().trim();
                    showWaitingDialog(R.string.waiting_reset_pwd, false, true);
                    ServerApiFactory.getApiImpl().resetPassword(name, pwd, code,
                            new RequestCallback() {

                                @Override
                                public void onSuccess(String rawData, Object response) {
                                    BaseResponse baseResponse = (BaseResponse) response;
                                    if (baseResponse.getStatus()
                                            .equals(Constants.REQUEST_SUCCESS)) {
                                        Preferences.savePassword(FindPwdActivity.this,
                                                pwd);
                                        setResult(RESULT_OK);
                                        finish();
                                    }
                                    showToast(baseResponse.getDesc());
                                    dismissDialog();
                                }

                                @Override
                                public void onError(int statusCode, Throwable e, String rawData) {
                                    dismissDialog();
                                    if (NetworkUtils.isNetConnect(getApplicationContext())) {
                                        showToast(R.string.oper_error);
                                    } else {
                                        showToast(R.string.net_error);
                                    }

                                }
                            });
                }
                break;

            default:
                break;
        }
    }

    private boolean check() {

        if (password.getText().toString().trim().length() != password.getText().toString().length()) {
            showToast(R.string.pwd_not_has_space);
            return false;
        }

        if (password.getText().toString().length() == 0) {
            showToast(R.string.pwd_null);
            return false;
        } else if (password.getText().toString().length() < 6) {
            showToast(R.string.pwd_format);
            return false;
        }

        if (rePassword.getText().toString().length() == 0) {
            showToast(R.string.re_pwd_null);
            return false;
        } else if (!rePassword.getText().toString()
                .equals(password.getText().toString())) {
            showToast(R.string.re_pwd_format);
            return false;
        }
        
        if (editCode.getText().toString().trim().length() == 0) {
            showToast(R.string.code_null);
            return false;
        } else {
            if (editCode.getText().toString().trim().length() != 4) {
                showToast(R.string.code_format);
                return false;
            }
        }

        return true;
    }

    private class ButtonTimer extends CountDownTimer {

        /**
         * @param millisInFuture
         * @param countDownInterval
         */
        public ButtonTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            if (mButtonTimer != null) {
                mButtonTimer.cancel();
                mButtonTimer = null;
            }
            mReVerifyBtn.setEnabled(true);
            mReVerifyBtn.setText(R.string.re_verify_phone_number);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            long second = millisUntilFinished / 1000;
            mReVerifyBtn.setText(getResources().getString(
                    R.string.re_verify_phone_number_countdown, second));
        }
    }

}
